package com.springboot.demoboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.springboot.demoboot.entity.User;
import com.springboot.demoboot.service.UserService;
import freemarker.core.JSONOutputFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author bamboo
 * @Date 2019/4/17 10:52
 * @Version 1.0
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public JSONObject addUser(@RequestBody User user){

        int res = userService.insertUser(user);
        JSONObject resultJson = new JSONObject();
        if (res == 1){
            resultJson.put("code",200);
            resultJson.put("msg", "用户添加成功");
        }else {
            resultJson.put("code",500);
            resultJson.put("msg", "用户添加失败");
        }
        return resultJson;
    }

    @GetMapping("/findUser")
    public JSONObject findUser(){
        List<User> userList = userService.findUserList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", userList);
        jsonObject.put("code", 200);
        return jsonObject;
    }
}
